package com.mall.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.entity.TbItem;
import com.mall.entity.TbItmeSpec;
import com.mall.product.mapper.GoodsMapper;
import com.mall.product.mapper.ItemMapper;
import com.mall.product.mapper.ItemSpecMapper;
import com.mall.product.service.ItemService;
import com.mall.utils.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * 项服务impl
 * created with IntelliJ IDEA
 *
 * @author ASUS
 * @Auther: W
 * @Date: 2021/07/15/19:09
 * Other:
 * @date 2021/07/16
 */
@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private ItemSpecMapper itemSpecMapper;

    @Autowired
    private ItemMapper itemMapper;

    @Override
    public List<TbItem> findAll() {
        return itemMapper.findAll();
    }

    /**
     * 添加商品其他设置
     * @param item 商品对象
     * @return 是否成功
     */
    @Override
    public boolean addItem(TbItem item) {
        try {
            IdWorker idWorker = new IdWorker();
            item.setId(idWorker.nextId());
            // 默认添加商品 下架状态
            item.setStatus("2");
            // 商品真实销量  0
            item.setRealSales(0);
            // 上架时间 和修改时间为当前系统时间
            item.setCreateTime(new Date());
            item.setUpdateTime(new Date());
            if (item.getIsSingle() == 1) {
                //单规格商品信息添加
                item.setIsDefault("1");
                // 默认库存商品全部上架
                item.setDepotNum(item.getInventory());
                itemMapper.insertItem(item);
            } else {
                //多规格添加
                // 选择多种商品规格属性
                if (item.getItmeSpecList() != null) {

                    item.getItmeSpecList().stream().forEach(specItem ->{
                        specItem.setId(idWorker.nextId());
                        itemSpecMapper.insertSpec(specItem);
                    });
                    // 将商品规格总库存 添加商品信息
                    item.setInventory(item.getItmeSpecList().stream().mapToInt(TbItmeSpec::getInventory).sum());
                    // 默认 上架数量全部上架
                    item.setDepotNum(item.getItmeSpecList().stream().mapToInt(TbItmeSpec::getInventory).sum());
                    item.setIsDefault("1");
                    // 获取最小值保存
                    item.setPrice(item.getItmeSpecList().stream().filter(e -> e != null).
                            min(Comparator.comparing(TbItmeSpec::getPrice)).get().getPrice());
                    itemMapper.insertItem(item);

                }
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public PageInfo findPage(TbItem item, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbItem> itemList = itemMapper.findPage(item);
        itemList.stream().forEach(entity ->{
            entity.setTbGoods(goodsMapper.findById(entity.getGoodsId()));
        });
        PageInfo<TbItem> pageInfo = new PageInfo<>(itemList);
        return pageInfo;
    }

    @Override
    public boolean updateStatus(Long id,String status) {
        return itemMapper.updateStatus(id,status);
    }

    @Override
    public TbItem findOne(Long id) {
        TbItem one = itemMapper.findOne(id);
        one.setTbGoods(goodsMapper.findById(one.getGoodsId()));
        return one;
    }

    @Override
    public boolean upStatus(Long id, String status) {
        try{
            itemMapper.updateStatus(id,status);
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }

    }

    /**
     * 商品评论商品下拉框
     * @return
     */
    @Override
    public List<TbItem> find() {
        return itemMapper.find();
    }

    @Override
    public PageInfo findAllPage(TbItem item, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbItem> itemList = itemMapper.findAllPage(item);
        itemList.stream().forEach(entity ->{
            entity.setTbGoods(goodsMapper.findById(entity.getGoodsId()));
        });
        PageInfo<TbItem> pageInfo = new PageInfo<>(itemList);
        return pageInfo;
    }

    @Override
    public TbItem findByGoodsId(Long goodsId) {
        return itemMapper.findByGoodsId(goodsId);
    }

    @Override
    public void update(TbItem item) {
        itemMapper.update(item);
    }


}
